import { createRouter, createWebHistory } from 'vue-router'
import { checkLogin, checkLevel } from '../api/api';
import pinia from '../store';
import { userStore } from '../store/user';

const user = userStore(pinia)

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/Login',
      component: () => import('../views/TheLogin.vue'),
    },
    {
      path: '/',
      name: 'Chatpage',
      component: () => import('../views/ChatPage.vue'),
      beforeEnter: async (from, to) => {

        try {
          await checkLogin()
        } catch (error) {
          return false
        }

      }
    },
    {
      path: '/Administrator',
      name: 'Administrator',
      component: () => import('../views/TheAdministrator.vue'),
      beforeEnter: async (from, to) => {
        console.log(user.getLevel);
        if (user.getLevel != 1) {
          return false
        }
      }
    },
    {
      path: "/ChangePassword",
      name: 'ChangePassword',
      component: () => import('../views/MyPage.vue')
    }
  ]
})


export default router
